package net.caixiaomi.info.Lottery.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.caixiaomi.info.Lottery.callback.BallCallBack;
import net.caixiaomi.info.Lottery.callback.LotteryMainCallBack;
import net.caixiaomi.info.Lottery.model.BallItem;
import net.caixiaomi.info.Lottery.model.LotteryBettingEntity;
import net.caixiaomi.info.Lottery.model.LotteryTicketInfo;
import net.caixiaomi.info.Lottery.presenter.BallPresenter;
import net.caixiaomi.info.Lottery.ui.BallLotteryMainActivity;
import net.caixiaomi.info.Lottery.ui.HistoryPopupWindow;
import net.caixiaomi.info.Lottery.view.BallView;
import net.caixiaomi.info.R;
import net.caixiaomi.info.WebViewActivity;
import net.caixiaomi.info.util.Constants;

import java.util.List;


/**
 * Created by suntian on 2018/8/6.
 */

public class CourageDragFragment extends Fragment implements View.OnClickListener{

    private BallView mBeforeDanView;
    private BallView mBeforeTuoView;
    private BallView mAfterDanView;
    private BallView mAfterTuoView;
    private TextView mDateTv;
    private TextView mPrizes;
    private BallPresenter mPresenter;
    private LotteryMainCallBack mMainCallBack;

    private HistoryPopupWindow mPopupWindow;

    private boolean mControl;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=View.inflate(getActivity(), R.layout.ball_select_layout,null);
        view.findViewById(R.id.ball_select_what_dantuo).setOnClickListener(this);
        mBeforeDanView= view.findViewById(R.id.ballview1);
        mBeforeTuoView= view.findViewById(R.id.ballview2);
        mAfterDanView= view.findViewById(R.id.ballview3);
        mAfterTuoView= view.findViewById(R.id.ballview4);
        mDateTv=view.findViewById(R.id.lottery_date_des);
        mPrizes=view.findViewById(R.id.lottery_history_prizes);
        mPresenter=new BallPresenter(getActivity());
        mBeforeDanView.init(getResources().getColor(R.color.white), getResources().getColor(R.color.red), getResources().getColor(R.color.red), getResources().getColor(R.color.white), BallLotteryMainActivity.BALL_BEFORE_DAN, mPresenter);
        mBeforeTuoView.init(getResources().getColor(R.color.white), getResources().getColor(R.color.red), getResources().getColor(R.color.red), getResources().getColor(R.color.white),BallLotteryMainActivity.BALL_BEFORE_TUO,mPresenter);
        mAfterDanView.init(getResources().getColor(R.color.white), getResources().getColor(R.color.blue), getResources().getColor(R.color.blue), getResources().getColor(R.color.white),BallLotteryMainActivity.BALL_AFTER_DAN, mPresenter);
        mAfterTuoView.init(getResources().getColor(R.color.white), getResources().getColor(R.color.blue), getResources().getColor(R.color.blue), getResources().getColor(R.color.white),BallLotteryMainActivity.BALL_AFTER_TUO, mPresenter);
        mPresenter.initCourageView(mBeforeDanView,mBeforeTuoView,mAfterDanView,mAfterTuoView,1,mMainCallBack);

        view.findViewById(R.id.lottery_history_btn).setOnClickListener(this);
        mPopupWindow=new HistoryPopupWindow();
        mPopupWindow.init(getActivity());
        mBeforeDanView.setMissSwitch(mControl);
        mBeforeTuoView.setMissSwitch(mControl);
        mAfterDanView.setMissSwitch(mControl);
        mAfterTuoView.setMissSwitch(mControl);
        return view;
    }

    public void setCallBack(LotteryMainCallBack callBack){
        mMainCallBack=callBack;
    }

    public void setNumMissControl(boolean control){
        mControl=control;
        if(mBeforeDanView!=null) {
            mBeforeDanView.setMissSwitch(control);
            mBeforeTuoView.setMissSwitch(control);
            mAfterDanView.setMissSwitch(control);
            mAfterTuoView.setMissSwitch(control);
        }
    }

    public void setInfo(LotteryTicketInfo info){
        mDateTv.setText(info.getTerm_num()+"期 截止时间 "+info.getEndDate());
        mPrizes.setText(info.getPrizes());
        mPopupWindow.setData(info.getPrizeList());
    }

    public void clearBall(){
        mBeforeDanView.setCheckData(null,true);
        mBeforeTuoView.setCheckData(null,true);
        mAfterDanView.setCheckData(null,true);
        mAfterTuoView.setCheckData(null,true);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.lottery_history_btn:
                mPopupWindow.showHistoryPopupWindow(mDateTv);
                break;
            case R.id.ball_select_what_dantuo:
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(Constants.BASE_H5_URL).append("daletou/whatDantuo");
                String url = stringBuilder.toString();
                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url", url);
                startActivity(intent);
                break;
        }
    }

    public boolean addBetingData(){
        return mPresenter.addBettingData();
    }

    public boolean isNoBallCheck(){
        return mPresenter.isNoBallCheck();
    }
}
